#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void *thread1(void *arg)
{
    printf("%s returning ... \n",(char*)arg);
    
    int *p = (int*)malloc(sizeof(int));
    *p = 1;
    
    return (void*)p;
}
void *thread2(void *arg)
{
    printf("%s exiting ...\n",(char*)arg);
    
    int *p = (int*)malloc(sizeof(int));
    *p = 2;
    
    pthread_exit((void*)p);
} 
void *thread3(void *arg)
{
    while ( 1 ){ //
        printf("%s is running ...\n",(char*)arg);
        sleep(1);
    } 
    return NULL;
} 
int main( void )
{
    pthread_t tid;
    void *ret;
    // thread 1 return
    pthread_create(&tid, NULL, thread1, (void*)"thread 1");
    pthread_join(tid, &ret);
    printf("thread 1 return, thread id %X, return code:%d\n", tid, *(int*)ret);
    free(ret);
    // thread 2 exit
    pthread_create(&tid, NULL, thread2, (void*)"thread 2");
    pthread_join(tid, &ret);
    printf("thread 2 exit, thread id %X, return code:%d\n", tid, *(int*)ret);
    free(ret);
    // thread 3 cancel by other
    pthread_create(&tid, NULL, thread3, (void*)"thread 3");
    sleep(3);
    pthread_cancel(tid);
    pthread_join(tid, &ret);
    if (ret == PTHREAD_CANCELED)
        printf("thread 3 cancel, thread id %X, return code: PTHREAD_CANCELED->%d\n", tid,ret);
    else
        printf("thread return, thread id %X, return code:%d\n", tid,ret);
    return 0;
}
